package phoenics.manpads.stinger.controller;

import de.felixroske.jfxsupport.FXMLController;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.image.ImageView;
import javafx.stage.Modality;
import org.springframework.beans.factory.annotation.Autowired;
import phoenics.manpads.stinger.StingerFxApplicaction;
import phoenics.manpads.stinger.config.IconConfig;
import phoenics.manpads.stinger.dao.ProjectDao;
import phoenics.manpads.stinger.domain.po.Project;
import phoenics.manpads.stinger.model.ProjectModel;
import phoenics.manpads.stinger.model.ProjectTreeModel;
import phoenics.manpads.stinger.view.EditProjectModalDialog;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @author Phoenics Chow
 * on 2019年01月19日
 */

@FXMLController
public class MainController implements Initializable {
    private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(MainController.class);
    @FXML
    TreeView<ProjectTreeModel> projectTree;
    @FXML
    ContextMenu funMenu;
    @Autowired
    ProjectDao projectDao;
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        reloadProjectTree();
        //projectTree.setCellFactory(this::call);
        /*
        TreeItem<String> item = new TreeItem<>("跟节点");
        //projectItem.setRoot(item);
        item.setExpanded(false);
        TreeItem<String> i1 = new TreeItem<>("电影");
        TreeItem<String> i2 = new TreeItem<>("音乐");
        TreeItem<String> i3 = new TreeItem<>("游戏");
        item.getChildren().addAll(i1,i2,i3);
        TreeItem<String> i4 = new TreeItem<>("荡寇风云");
        TreeItem<String> i5 = new TreeItem<>("变形金刚5");
        i1.setExpanded(false);
        i1.getChildren().addAll(i4,i5);

        TreeItem<String> item2 = new TreeItem<>("跟节点2");
        //projectItem.setRoot(item2);

        item2.setExpanded(false);
        TreeItem<String> i12 = new TreeItem<>("电影2");
        TreeItem<String> i22 = new TreeItem<>("音乐2");
        TreeItem<String> i32 = new TreeItem<>("游戏2");
        item2.getChildren().addAll(i12,i22,i32);
        TreeItem<String> i42 = new TreeItem<>("荡寇风云2");
        TreeItem<String> i52 = new TreeItem<>("变形金刚52");
        i12.setExpanded(false);
        i12.getChildren().addAll(i42,i52);
        TreeItem<String> dummyRoot = new TreeItem<>();
        dummyRoot.getChildren().addAll(item, item2);
        //TreeView<String> tree = new TreeView<>(dummyRoot);
        projectItem.setShowRoot(false);
        projectItem.setRoot(dummyRoot);
        */

    }
    @FXML
    public void createProject(ActionEvent event){
        EditProjectController.projectModel.clear();
        StingerFxApplicaction.showView(EditProjectModalDialog.class, Modality.APPLICATION_MODAL);

    }
    public void reloadProjectTree(){
        List<Project> projects=projectDao.selectAll();
        final TreeItem<ProjectTreeModel> dummyRoot = new TreeItem<>();
        projectTree.setShowRoot(false);
        projectTree.setRoot(dummyRoot);
        dummyRoot.getChildren().clear();
        projects.forEach(p->{
            ProjectTreeModel ti=new ProjectTreeModel();
            ti.setName(p.getProjectCnName());
            ti.setData(p);
            TreeItem<ProjectTreeModel> t=new TreeItem<>(ti, IconConfig.projectIcon());
            dummyRoot.getChildren().add(configProjectNode(t));

        });

    }
    private TreeItem<ProjectTreeModel> configProjectNode(TreeItem<ProjectTreeModel> treeItemPartent){
        TreeItem<ProjectTreeModel> brother1=configProjectFunctionNode("数据库表",treeItemPartent.getValue().getData(),IconConfig.tableIcon());
        TreeItem<ProjectTreeModel> brother2=configProjectFunctionNode("表间关系",treeItemPartent.getValue().getData(),IconConfig.raIcon());
        treeItemPartent.getChildren().addAll(brother1,brother2);
        return treeItemPartent;
    }
    private TreeItem<ProjectTreeModel> configProjectFunctionNode( String name,Object object,  ImageView icon){
        ProjectTreeModel tableRoot=new ProjectTreeModel();
        tableRoot.setName(name);
        tableRoot.setData(object);
        return new TreeItem<>(tableRoot, icon);
    }
    public void showingMenu(){
        funMenu.getItems().get(0).setVisible(true);



    }

}
